﻿using Api.Application.Command.User;
using Api.DTO;
using Domain;
using ErrorCode;
using Infrastructure;
using MediatR;

namespace Api.Application.Handler.User
{
    /// <summary>
    /// 中介者--获取用户列表处理类
    /// </summary>
    public class ShowUserHandler : IRequestHandler<ShowUserCommand, ApiPaging<UserDTO>>
    {
        private readonly IBase<UserModel> userRepo;
        private readonly IBase<RegionModel> regionRepo;
        private readonly IBase<ProvinceModel> provinceRepo;
        private readonly IBase<CityModel> cityRepo;
        private readonly IBase<UserRoleModel> userRoleRepo;
        private readonly IBase<RoleModel> roleRepo;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userRepo"></param>
        /// <param name="regionRepo"></param>
        /// <param name="provinceRepo"></param>
        /// <param name="cityRepo"></param>
        public ShowUserHandler(IBase<UserModel> userRepo, IBase<RegionModel> regionRepo, IBase<ProvinceModel> provinceRepo, IBase<CityModel> cityRepo,IBase<UserRoleModel> userRoleRepo,IBase<RoleModel> roleRepo)
        {
            this.userRepo = userRepo;
            this.regionRepo = regionRepo;
            this.provinceRepo = provinceRepo;
            this.cityRepo = cityRepo;
            this.userRoleRepo = userRoleRepo;
            this.roleRepo = roleRepo;
        }
        public Task<ApiPaging<UserDTO>> Handle(ShowUserCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<UserDTO> paging = new ApiPaging<UserDTO>();
            var user = userRepo.GetAll().Where(x => x.IsDeleted == false);
            //
            var region = regionRepo.GetAll();
            //
            var city = cityRepo.GetAll();
            //
            var province = provinceRepo.GetAll();
            var userRole = userRoleRepo.GetAll();
            var role = roleRepo.GetAll();
            var linq = (from a in user
                       join b in region on a.RegionId equals b.Id
                       join c in city on b.CityId equals c.Id
                       join d in province on c.ProvinceId equals d.Id
                       join e in userRole on a.Id equals e.UserId into ae 
                       from e in ae.DefaultIfEmpty()
                       join f in role  on e.RoleId equals f.Id into ef 
                       from f in ef.DefaultIfEmpty()
                       select new UserDTO()
                       {
                           Id=a.Id,
                           UserName=a.UserName,
                           Sex=a.Sex,
                           ProvinceId=d.Id,
                           ProcinceName=d.ProcinceName,
                           CityId=c.Id,
                           CityName=c.CityName,
                           RegionId=b.Id,
                           RegionName=b.RegionName,
                           Assdr=a.Assdr,
                           CrearName=a.CrearName,
                           CrearTime=a.CrearTime,
                           UpdateName=a.UpdateName,
                           UpdateTime=a.UpdateTime,
                           RoleId=f.Id,
                           RoleName=f.RoleName
                       }).ToList();
            var resData= linq.OrderByDescending(x => x.CrearTime).Skip((request.index - 1) * request.size).Take(request.size);
            var mergeData = new Dictionary<int,UserDTO>();
            foreach (var item in resData)
            {
                if(!mergeData.TryGetValue(item.Id,out var mergedItem))
                {
                    mergedItem = new UserDTO
                    {
                        Id = item.Id,
                        UserName = item.UserName,
                        Sex = item.Sex,
                        ProcinceName = item.ProcinceName,
                        CityName = item.CityName,
                        RegionName = item.RegionName,
                        Assdr = item.Assdr,
                        CrearName = item.CrearName,
                        CrearTime = item.CrearTime,
                        UpdateName = item.UpdateName,
                        UpdateTime = item.UpdateTime,
                        CityId = item.CityId, 
                        ProvinceId = item.ProvinceId,
                        RegionId = item.RegionId,
                        RoleId = item.RoleId,
                        RoleName = item.RoleName
                    };
                    mergeData[item.Id] = mergedItem;
                }
                else
                {
                    mergedItem.RoleName += "、"+item.RoleName;
                }
            }
            paging.TotalCount = linq.Count();
            paging.TotalPage = (int)Math.Ceiling(paging.TotalCount * 1.0 / request.size);
            paging.Data = mergeData.Values.ToList();
            paging.Code = ApiEnum.OK;
            paging.Msg = "获取列表成功";
            return Task.FromResult(paging);
        }
    }
}
